package com.vehicle.controller;
import com.vehicle.bean.User;
import com.vehicle.feignForm.ContactForm;
import com.vehicle.form.UpdateUserForm;
import com.vehicle.form.UserLoginForm;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.UserService;
import com.vehicle.util.JwtUtil;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.ResultVO;
import com.vehicle.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

//    @Autowired
//    private RedisTemplate redisTemplate;

    @Autowired
    private UserLogMapper userLogMapper;


    //用户登录: 电话和密码
    @PostMapping("/login")
    public ResultVO login(HttpServletRequest request,@RequestBody UserLoginForm userForm){
        String ip = request.getRemoteAddr();
        System.out.println(userForm);
        User user = userService.getUserExistByTel(userForm.getTel());
//        //验证密码
//        if(!MD5Util.getSaltverifyMD5(userForm.getPassword(),user.getPassword())){
//            throw new VehicleException(ResponseEnum.PASSWORD_ERROR.getMsg());
//        }
        //生成token
        String token = JwtUtil.createToken(user.getUserId(),user.getTel());
        //修改数据库里面的token
        UserLoginVO userVO = userService.updateToken(user,token);
        if(userVO == null){
            userLogMapper.insert(LogUtil.insertUserLogin(user.getUserId(),"登陆失败",ip,"失败"));
        }
        userLogMapper.insert(LogUtil.insertUserLogin(user.getUserId(),"登陆成功",ip,"成功"));
        System.out.println("是这里回不来吗？");
        System.out.println(userVO);
        return ResultVO.succ(userVO);
    }

//        @GetMapping("/checkToken/{token}")
//    public ResultVO checkToken(@PathVariable("token") String token){
//        Boolean b1 = JwtUtil.checkToken(token);
//        if(!b1){
//            throw new VehicleException(ResponseEnum.TOKEN_ERROR.getMsg());
//        }
//        return ResultVO.succ(null);
//    }
//    @GetMapping("/checkToken")
//    public ResultVO checkToken(HttpServletRequest request){
//        String token = request.getHeader("token");
//        boolean result = JwtUtil.checkToken(token);
//        if(result) return ResultVO.succ(null);
//        return ResultVO.fail(ResponseEnum.TOKEN_ERROR.getMsg());
//    }


    //修改用户信息
    @PutMapping("/userMessage")
    public ResultVO updateUserMessage(UpdateUserForm userForm){
        boolean b = userService.updateUserBaseMessage(userForm);
        if(b){
            return ResultVO.succ("修改成功");
        }
        return  ResultVO.fail("修改失败");
    }

    //添加联系人
    @PostMapping("/contact")
    public ResultVO addUserContact(HttpServletRequest request,ContactForm contactForm) {
        String token = request.getHeader("token");
        Boolean b = userService.createContactByToken(token,contactForm);
        if(b){
            return ResultVO.succ("添加成功");
        }
       return ResultVO.fail("添加失败");
    }

    //修改联系人
    @PutMapping("/contact")
    public ResultVO updateContactUserContact(ContactForm contactForm){
        Boolean b = userService.updateContactByToken(contactForm);
        if(b){
            return ResultVO.succ("修改成功");
        }
        return ResultVO.fail("修改失败");
    }
    //删除联系人
    @DeleteMapping("/contact")
    public ResultVO deleteUserContact(HttpServletRequest request){
        String token = request.getHeader("token");
        Boolean b = userService.deleteContactByToken(token);
        if(b){
            return ResultVO.succ("删除成功");
        }
        return ResultVO.fail("删除失败");
    }

    //获取所有用户列表
    @GetMapping("/userList/{page}/{size}")
    public ResultVO getUserList(@PathVariable("page") String page,@PathVariable("size") String size){
        return ResultVO.succ(userService.getAllUserList(Integer.parseInt(page),Integer.parseInt(size)));
    }

    @GetMapping("/userList/{searchValue}")
    public ResultVO getAdminListBySearchContent(@PathVariable("searchValue") String searchValue){
        return ResultVO.succ(userService.getListByContent(searchValue));
    }

    @GetMapping("/getTel/{userId}")
    public String getUserTel(@PathVariable("userId") Integer userId){
        return userService.getTelById(userId);
    }

    @GetMapping("/getName/{userId}")
    public String getUserName(@PathVariable("userId") Integer userId){
        return userService.getUserNameById(userId);
    }
    //删除用户
    @DeleteMapping("/deleteOne/{userId}")
    public ResultVO deleteUser(@PathVariable("userId") Integer userId){
        boolean b = userService.deleteUserById(userId);
        if(b){
            return ResultVO.succ(null);
        }
        return ResultVO.fail("删除失败");
    }

    //添加一个用户
    @PostMapping("/oneUser")
    public ResultVO addOneUser(@RequestBody User user){
        System.out.println("user内容为：");
        System.out.println(user);
        return ResultVO.succ(userService.addOnlyOneUser(user));
    }

    //以文件方式添加一群用户
    @PostMapping("/addListUser")
    public ResultVO addListUser(){
        return ResultVO.succ(null);
    }


}

